package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrankService;
import com.dongdongshop.service.SpecService;
import com.dongdongshop.service.TypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    @DubboReference
    private TypeTemplateService templateService;

    @DubboReference
    private BrankService brankService;

    @DubboReference
    private SpecService specService;


    @RequestMapping("toShow")
    public String toShow(Model model){
        List<TbBrand> brandList =  brankService.toShow();
        List<TbSpecification> specList = specService.toShow();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specList);
        return "/admin/type_template";
    }

    @RequestMapping("findList")
    @ResponseBody
    public Result findList(@RequestParam(defaultValue = "1")Integer pageNum,
                           @RequestParam(defaultValue = "3")Integer pageSize,
                           TbTypeTemplate typeTemplate){
        Page<TbTypeTemplate> page = templateService.fdindList(pageNum,pageSize,typeTemplate);
        return Result.ok().setData(page);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbTypeTemplate typeTemplate){
        templateService.insert(typeTemplate);
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(Long[] ids){
        templateService.deleteInfo(ids);
        return Result.ok();
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        TbTypeTemplate typeTemplate = templateService.toUpdate(id);
        return Result.ok().setData(typeTemplate);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbTypeTemplate typeTemplate){
        Integer i = templateService.updateInfo(typeTemplate);
        return Result.response(ResultEnum.SUCCESS);
    }
}
